﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Util;
using FastIOC.Annotation;
using FastORM.Attribute;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("OU")]
    public class OUController : FastTemplateController<Frame_OU>
    {
        [AutoWired]
        public DB_User @DB_User;

        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/OU/OU_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/OU/OU_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/OU/OU_Edit.cshtml");
        }

        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {

            JObject Params = GetParams();
            string OUName = Convert.ToString(Params["OUName"]);
            string ParentOUCode = Convert.ToString(Params["ParentOUCode"]);
            bool SearchInAll = Convert.ToBoolean(Params["SearchInAll"]);
            if (SearchInAll)
            {
                if (!string.IsNullOrEmpty(OUName))
                {
                    ListWhereText += string.Format(" and OUName like '%{0}%'", OUName);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(OUName))
                {
                    ListWhereText += string.Format(" and OUName like '%{0}%'", OUName);
                }
                if (!string.IsNullOrEmpty(ParentOUCode))
                {
                    if (ParentOUCode == "Top")
                    {
                        ListWhereText += string.Format(" and LENGTH(OUCode)={0}", 4);
                    }
                    else
                    {
                        ListWhereText += string.Format(" and LENGTH(OUCode)={0} and OUCode like '{1}%'", ParentOUCode.Length + 4, ParentOUCode);
                    }
                    
                }
            }

            return base.FindList();
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_OU Entity)
        {
            return base.OperateAdd(Entity);
        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            bool IsAuth = true;
            string keyguidlist = Convert.ToString(Params["KeyGuidList"]);
            string[] keyguidarray = keyguidlist.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string uguid in keyguidarray)
            {
                List<Frame_OU> subou = DB_Fast.FindAll(string.Format(" and ParentOUGuid='{0}'", uguid));
                if (subou.Count > 0)
                {
                    IsAuth = false;
                    result.Add("code", -100);
                    result.Add("msg", "选择的部门有下级部门，无法删除！");
                    break;
                }

                List<Frame_User> subuser = @DB_User.FindAll(string.Format(" and OUGuid='{0}'", uguid));
                if (subuser.Count > 0)
                {
                    IsAuth = false;
                    result.Add("code", -200);
                    result.Add("msg", "选择的部门有人员，无法删除！");
                    break;
                }
            }
            if (IsAuth)
            {
                DB_Fast.BatchDelete(keyguidarray.ToList());
            }
            return result;
        }

        [Route("Tree")]
        public ActionResult Tree()
        {
            ReturnValue result = new ReturnValue();
            List<Frame_OU> AllOUList = DB_Fast.FindAll("", "OrderNum desc");
            List<Tree> AllTreeList = new List<Tree>();
            foreach (Frame_OU item in AllOUList)
            {
                Tree treenode = new Tree();
                treenode.id = item.RowGuid;
                treenode.label = item.OUName;
                treenode.paerntGuid = item.ParentOUGuid;
                treenode.AddAtttibute("oucode", item.OUCode);
                AllTreeList.Add(treenode);
            }
            List<Tree> TreeNodeList = TreeBuilder.Build("Top", AllTreeList);
            result.Add("tree", TreeNodeList);
            return result;
        }
    }
}
